package com.lin.pay.ali.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.lin.common.constant.CommonConstant;
import com.lin.common.error.CustomRuntimeException;
import com.lin.common.error.ErrorCode;
import com.lin.pay.ali.ALIPayConstant;
import com.lin.properties.PayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class AliServiceImpl implements AliService {
    private static final Logger log = LoggerFactory.getLogger(AliServiceImpl.class);

    @Autowired
    private PayProperties payProperties;

    @Override
    public AlipayTradeAppPayResponse appPay(String outTradeNo, String totalAmount, String subject) throws CustomRuntimeException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY), payProperties.getAlipay().get(PayProperties.ALIPAY_APPID), payProperties.getAlipay().get(PayProperties.ALIPAY_MYPRIVATEKEY), CommonConstant.FORMAT_JSON, CommonConstant.CHARSET_UTF8, payProperties.getAlipay().get(PayProperties.ALIPAY_ALI_PUBLIC), payProperties.getAlipay().get(PayProperties.ALIPAY_SIGNTYPE));
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(buildAppPayRequest(outTradeNo, totalAmount, subject));
        if (response.isSuccess()) {
            return response;
        } else {
            log.error("send apppay error code: {}  \n message: {}", response.getCode(), response.getMsg());
            throw new CustomRuntimeException(ErrorCode.PAY_FAIL, ErrorCode.PAY_FAIL.getMessage());
        }
    }

    private AlipayTradeAppPayRequest buildAppPayRequest(String outTradeNo, String totalAmount, String subject) throws AlipayApiException, CustomRuntimeException {
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        request.setNotifyUrl(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY));
        JSONObject bizContent = new JSONObject();
        bizContent.put(ALIPayConstant.out_trade_no, outTradeNo);
        bizContent.put(ALIPayConstant.total_amount, totalAmount);//0.01
        bizContent.put(ALIPayConstant.subject, subject);
        bizContent.put(ALIPayConstant.product_code, "QUICK_MSECURITY_PAY");
        request.setBizContent(bizContent.toString());
        return request;
    }

    @Override
    public AlipayTradeQueryResponse query(String out_trade_no) throws AlipayApiException, CustomRuntimeException {
        AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY), payProperties.getAlipay().get(PayProperties.ALIPAY_APPID), payProperties.getAlipay().get(PayProperties.ALIPAY_MYPRIVATEKEY), CommonConstant.FORMAT_JSON, CommonConstant.CHARSET_UTF8, payProperties.getAlipay().get(PayProperties.ALIPAY_ALI_PUBLIC), payProperties.getAlipay().get(PayProperties.ALIPAY_SIGNTYPE));
        AlipayTradeQueryResponse response = alipayClient.execute(buildqueryRequest(out_trade_no));
        if (response.isSuccess()) {
            return response;
        } else {
            log.error("send query error code: {}  \n message: {}", response.getCode(), response.getMsg());
            throw new CustomRuntimeException(ErrorCode.PAY_FAIL, ErrorCode.PAY_FAIL.getMessage());
        }
    }

    private AlipayTradeQueryRequest buildqueryRequest(String out_trade_no) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put(ALIPayConstant.out_trade_no, out_trade_no);
        request.setBizContent(bizContent.toString());
        return request;
    }

    @Override
    public AlipayTradeRefundResponse refund(String trade_no, String refund_amount, String out_request_no) throws AlipayApiException, CustomRuntimeException {
        AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY), payProperties.getAlipay().get(PayProperties.ALIPAY_APPID), payProperties.getAlipay().get(PayProperties.ALIPAY_MYPRIVATEKEY), CommonConstant.FORMAT_JSON, CommonConstant.CHARSET_UTF8, payProperties.getAlipay().get(PayProperties.ALIPAY_ALI_PUBLIC), payProperties.getAlipay().get(PayProperties.ALIPAY_SIGNTYPE));
        AlipayTradeRefundResponse response = alipayClient.execute(buildRefundRequest(trade_no, refund_amount, out_request_no));
        if (response.isSuccess()) {
            return response;
        } else {
            log.error("send query error code: {}  \n message: {}", response.getCode(), response.getMsg());
            throw new CustomRuntimeException(ErrorCode.PAY_FAIL, ErrorCode.PAY_FAIL.getMessage());
        }
    }

    private AlipayTradeRefundRequest buildRefundRequest(String trade_no, String refund_amount, String out_request_no) {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put(ALIPayConstant.trade_no, trade_no);
        bizContent.put(ALIPayConstant.refund_amount, refund_amount);
        bizContent.put(ALIPayConstant.out_request_no, out_request_no);
        request.setBizContent(bizContent.toString());
        return request;
    }

    @Override
    public AlipayTradeFastpayRefundQueryResponse refundquery(String trade_no, String out_request_no) throws AlipayApiException, CustomRuntimeException {
        AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY), payProperties.getAlipay().get(PayProperties.ALIPAY_APPID), payProperties.getAlipay().get(PayProperties.ALIPAY_MYPRIVATEKEY), CommonConstant.FORMAT_JSON, CommonConstant.CHARSET_UTF8, payProperties.getAlipay().get(PayProperties.ALIPAY_ALI_PUBLIC), payProperties.getAlipay().get(PayProperties.ALIPAY_SIGNTYPE));
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(buildRefundQueryRequest(trade_no, out_request_no));
        if (response.isSuccess()) {
            return response;
        } else {
            log.error("send query error code: {}  \n message: {}", response.getCode(), response.getMsg());
            throw new CustomRuntimeException(ErrorCode.PAY_FAIL, ErrorCode.PAY_FAIL.getMessage());
        }
    }

    private AlipayTradeFastpayRefundQueryRequest buildRefundQueryRequest(String trade_no, String out_request_no) {
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put(ALIPayConstant.trade_no, trade_no);
        bizContent.put(ALIPayConstant.out_request_no, out_request_no);
        //// 返回参数选项，按需传入
        //JSONArray queryOptions = new JSONArray();
        //queryOptions.add("refund_detail_item_list");
        //bizContent.put("query_options", queryOptions);
        request.setBizContent(bizContent.toString());
        return request;
    }

    @Override
    public AlipayTradeCloseResponse close(String trade_no) throws AlipayApiException, CustomRuntimeException {
        AlipayClient alipayClient = new DefaultAlipayClient(payProperties.getAlipay().get(PayProperties.ALIPAY_GATEWAY), payProperties.getAlipay().get(PayProperties.ALIPAY_APPID), payProperties.getAlipay().get(PayProperties.ALIPAY_MYPRIVATEKEY), CommonConstant.FORMAT_JSON, CommonConstant.CHARSET_UTF8, payProperties.getAlipay().get(PayProperties.ALIPAY_ALI_PUBLIC), payProperties.getAlipay().get(PayProperties.ALIPAY_SIGNTYPE));
        AlipayTradeCloseResponse response = alipayClient.execute(buildCloseRequest(trade_no));
        if (response.isSuccess()) {
            return response;
        } else {
            log.error("send query error code: {}  \n message: {}", response.getCode(), response.getMsg());
            throw new CustomRuntimeException(ErrorCode.PAY_FAIL, ErrorCode.PAY_FAIL.getMessage());
        }
    }

    private AlipayTradeCloseRequest buildCloseRequest(String trade_no) {
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put(ALIPayConstant.trade_no, trade_no);
        request.setBizContent(bizContent.toString());
        return request;
    }


}
